package com.gyp.utils;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

/**
 * @Description Redisson工具类
 * @author xyb
 * @since 10:02 2023/8/17
 */
@Component
@Slf4j
public class RedissonUtils implements ApplicationContextAware {

    public static RedissonClient redissonClient;

    /**
     * @Description 加锁执行
     * @author xyb
     * @since 10:06 2023/8/17
     */
    public static  <T> T lockExe(String key, Supplier<T> supplier){
        RLock lock = redissonClient.getLock(key);
        try {
            lock.lock();
            return supplier.get();
        }
        catch (Exception e){
            log.error("[加锁执行出现异常:{}]",e);
            throw e;
        }
        finally {
            if(lock.isHeldByCurrentThread() && lock.isLocked()){
                lock.unlock();
            }
        }
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        redissonClient = applicationContext.getBean(RedissonClient.class);
    }
}
